package cn.elwy.rcp.kit.maven;

import java.io.File;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.di.Persist;
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import cn.elwy.rcp.kit.intro.ServiceTest;

public class MavenKit {

	private static final String MAVEN_REPO_PATH = "X:/Repository";
	private static final String ENCODE = "gbk";
	private Text txtInput;
	private DeleteLastUpdatedHandler deleteLastUpdatedHandler;
	private Text txtXrepository;

	@Inject
	private MDirtyable dirty;
	private Text text;
	private Text text_1;
	private Table table;

	@PostConstruct
	public void createComposite(Composite parent) {
		parent.setLayout(new GridLayout(1, false));
		// setTitle("Maven安装Jar");
		// setMessage("部署jar包到服务器或者本地");
		// Composite area = (Composite) super.createDialogArea(parent);
		// Composite container = new Composite(area, SWT.NONE);
		// GridLayout gl_container = new GridLayout(1, false);
		// container.setLayout(gl_container);
		// container.setLayoutData(new GridData(GridData.FILL_BOTH));

		TabFolder tabFolder = new TabFolder(parent, SWT.NONE);
		tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		TabItem tbtmNewItem = new TabItem(tabFolder, SWT.NONE);
		tbtmNewItem.setText("安装JAR包");

		Composite composite = new Composite(tabFolder, SWT.NONE);
		tbtmNewItem.setControl(composite);
		composite.setLayout(new GridLayout(3, false));

		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("JAR目录：");

		text = new Text(composite, SWT.BORDER);
		text.setText(
				"mvn install:install-file -Dfile=${file} -DgroupId=${groupId} -DartifactId=${artifactId} -Dversion=${version} -Dpackaging=jar");
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_1.setText("JAR目录：");

		text_1 = new Text(composite, SWT.BORDER);
		text_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setText("New Button");

		ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));

		ToolItem toolItem = new ToolItem(toolBar, SWT.NONE);
		toolItem.setText("添加文件夹");

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.setText("添加文件");

		ToolItem toolItem_1 = new ToolItem(toolBar, SWT.NONE);
		toolItem_1.setText("删除");
		new Label(composite, SWT.NONE);

		table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TabItem tbtmNewItem_1 = new TabItem(tabFolder, SWT.NONE);
		tbtmNewItem_1.setText("删除lastUpdated");

		Composite composite_1 = new Composite(tabFolder, SWT.NONE);
		tbtmNewItem_1.setControl(composite_1);
		composite_1.setLayout(new GridLayout(3, false));

		Label lblNewLabel11 = new Label(composite_1, SWT.NONE);
		lblNewLabel11.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel11.setText("Maven本地仓库路径：");

		txtXrepository = new Text(composite_1, SWT.BORDER);
		txtXrepository.setText(MAVEN_REPO_PATH);
		txtXrepository.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnNewButton11 = new Button(composite_1, SWT.NONE);
		btnNewButton11.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Display.getCurrent().asyncExec(new Runnable() {
					@Override
					public void run() {
						String text2 = txtXrepository.getText();
						if (text2 != null && !text2.isEmpty()) {
							File file = new File(text2);
							if (file.isDirectory()) {
								deleteLastUpdatedHandler.delFileRecr(file.listFiles(), null);
								deleteLastUpdatedHandler.out("清理lastUpdated完成！");
							}
						}
					}
				});
			}
		});
		btnNewButton11.setText("清理lastUpdated");

		text = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));

		deleteLastUpdatedHandler = new DeleteLastUpdatedHandler() {
			@Override
			public void out(String message) {
				text.append(message + "\r\n");
			}
		};
		// init();

		// return area;

	}

	@Focus
	public void setFocus() {
	}

	@Persist
	public void save() {
		dirty.setDirty(false);
	}
}